package com.hello.system.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.web.page.TableDataInfo;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.system.domain.UserTemplateUsage;
import com.hello.system.service.IUserTemplateUsageService;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户模板使用记录Controller
 * 
 */
@Slf4j
@RestController
@RequestMapping("/template/usage")
public class UserTemplateUsageController extends BaseController
{
    @Autowired
    private IUserTemplateUsageService userTemplateUsageService;

    /**
     * 查询用户模板使用记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserTemplateUsage userTemplateUsage)
    {
        startPage();
        List<UserTemplateUsage> list = userTemplateUsageService.selectUserTemplateUsageList(userTemplateUsage);
        return getDataTable(list);
    }
    
    /**
     * 获取用户模板使用记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userTemplateUsageService.selectUserTemplateUsageById(id));
    }

    /**
     * 新增用户模板使用记录
     */
    @Log(title = "用户模板使用记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserTemplateUsage userTemplateUsage)
    {
        return toAjax(userTemplateUsageService.insertUserTemplateUsage(userTemplateUsage));
    }

    /**
     * 修改用户模板使用记录
     */
    @Log(title = "用户模板使用记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserTemplateUsage userTemplateUsage)
    {
        return toAjax(userTemplateUsageService.updateUserTemplateUsage(userTemplateUsage));
    }

    /**
     * 删除用户模板使用记录
     */
    @Log(title = "用户模板使用记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userTemplateUsageService.deleteUserTemplateUsageByIds(ids));
    }
    
    /**
     * 检查模板使用是否免费
     */
    @GetMapping("/check")
    public AjaxResult checkFreeUsage(@RequestParam("userId") Long userId, @RequestParam("templateId") Long templateId)
    {
        boolean isFree = userTemplateUsageService.isFreeTemplateUsage(userId, templateId);
        int usageCount = userTemplateUsageService.getUsageCount(userId, templateId);
        return success(new FreeUsageResult(isFree, usageCount));
    }
    
    /**
     * 记录模板使用
     */
    @PostMapping("/record")
    @Log(title = "记录模板使用", businessType = BusinessType.INSERT)
    public AjaxResult recordUsage(@RequestParam("userId") Long userId, @RequestParam("templateId") Long templateId)
    {
        return toAjax(userTemplateUsageService.recordTemplateUsage(userId, templateId));
    }
    
    /**
     * 免费使用结果封装
     */
    static class FreeUsageResult {
        private boolean isFree;
        private int usageCount;
        
        public FreeUsageResult(boolean isFree, int usageCount) {
            this.isFree = isFree;
            this.usageCount = usageCount;
        }
        
        public boolean getIsFree() {
            return isFree;
        }
        
        public void setIsFree(boolean isFree) {
            this.isFree = isFree;
        }
        
        public int getUsageCount() {
            return usageCount;
        }
        
        public void setUsageCount(int usageCount) {
            this.usageCount = usageCount;
        }
    }
} 